package com.kunbo.cn.controller.system;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.kunbo.cn.authority.RequiredPermission;
import com.kunbo.cn.entity.system.Menu;
import com.kunbo.cn.service.system.MenuService;
import com.kunbo.cn.util.json.Json;
import com.kunbo.cn.util.retu.ReturnBean;
import com.kunbo.cn.util.string.QString;

@Controller  
@RequestMapping("/topJUI/index/*")  
public class MenuController {
	
    @Resource
    private MenuService menuService;
	
    //@ApiOperation(value="获取MENU项目", notes="")
	@RequestMapping(value="getMenu.do",produces="text/html; charset=UTF-8")  
    public @ResponseBody String getMenu(HttpServletRequest request,HttpServletResponse response){  
	    	String name = request.getParameter("name");    
	    	String position_id = request.getParameter("position_id");    	    
		String r="";
		r = Json.toJsonByPretty(menuService.getMenuByPid(name,position_id ));
		return r;
	}
	
	@RequestMapping(value="getMenuList.do",produces="text/html; charset=UTF-8")  
    public @ResponseBody String  getMenuList(HttpServletRequest request,HttpServletResponse response){      
		String id_str = request.getParameter("id");
		int id = 0;
		if(!QString.isNull(id_str))
			id = Integer.parseInt(id_str);  
		return Json.toJsonByPretty(menuService.getMenuList(id));
	}
	@RequestMapping(value="findById.do")  
    public @ResponseBody Menu findById(HttpServletRequest request,HttpServletResponse response){      
		String id = request.getParameter("id");
		return menuService.findById(Integer.parseInt(id));
	}
	
	@RequestMapping(value="save.do")  
    public @ResponseBody ReturnBean save(HttpServletRequest request,HttpServletResponse response){      
		String json = request.getParameter("data").replaceAll("\"\"", "null");
		Menu menu = (Menu) Json.toObject(Menu.class, json);
		boolean ret = menuService.save(menu);
		return new ReturnBean(ret,"");
	}
	
	@RequestMapping(value="delete.do")  
    public @ResponseBody ReturnBean delete(HttpServletRequest request,HttpServletResponse response){      
		String json = request.getParameter("data").replaceAll("\"\"", "null");
		Menu menu = (Menu) Json.toObject(Menu.class, json);
		boolean ret = menuService.delete(menu);
		return new ReturnBean(ret,"");
	}
	
	
	@RequestMapping("/getParentMenuName")
	public @ResponseBody String getParentMenuName(
			@RequestParam("menuName")String menuName
			){
		return menuService.getParentMenuName(menuName);
	}
	
	
	@RequestMapping("/getMenuNameByUrl")
	public @ResponseBody String getMenuNameByUrl(String url){
		Menu m = menuService.findByUrl(url);
		if(null==m) {
			return "";
		}else {
			return m.getText();
		}
		
	}
	
}
